package com.dalongxin.work;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;

import com.dalongxin.work.data.CloudDisk;
import com.dalongxin.work.data.CloudDiskUploadFile;
import com.dalongxin.work.data.Contact;
import com.dalongxin.work.data.Department;
import com.dalongxin.work.data.User;
import com.j256.ormlite.android.apptools.OrmLiteSqliteOpenHelper;
import com.j256.ormlite.dao.Dao;
import com.j256.ormlite.support.ConnectionSource;
import com.j256.ormlite.table.TableUtils;

import java.sql.SQLException;

public class DataHelper extends OrmLiteSqliteOpenHelper {

    private static DataHelper instance;
    private Dao<User, Integer> userDao = null;
    private Dao<CloudDisk, Integer> cloudDiskDao = null;
    private Dao<CloudDiskUploadFile, Integer> cloudDiskUploadFileDao = null;
    private Dao<Contact, Integer> contactDao = null;
    private Dao<Department, Integer> departmentDao = null;

    public DataHelper(Context context) {
        super(context, Constants.DATABASE_NAME, null, Constants.DATABASE_VERSION);
    }

    public static synchronized DataHelper getHelper(Context context) {
        if (instance == null) {
            synchronized (DataHelper.class) {
                if (instance == null) {
                    instance = new DataHelper(context);
                }
            }
        }
        return instance;
    }

    @Override
    public void onCreate(SQLiteDatabase sqLiteDatabase, ConnectionSource connectionSource) {
        try {
            TableUtils.createTableIfNotExists(connectionSource, User.class);
            TableUtils.createTableIfNotExists(connectionSource, CloudDisk.class);
            TableUtils.createTableIfNotExists(connectionSource, CloudDiskUploadFile.class);
            TableUtils.createTableIfNotExists(connectionSource, Contact.class);
            TableUtils.createTableIfNotExists(connectionSource, Department.class);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onUpgrade(SQLiteDatabase sqLiteDatabase, ConnectionSource connectionSource, int oldVersion, int newVersion) {
        if(oldVersion < 175) {
            //TableUtils.dropTable(connectionSource, AccountData.class, true);
        }
        try {
            TableUtils.dropTable(connectionSource, User.class, true);
            TableUtils.dropTable(connectionSource, CloudDisk.class, true);
            TableUtils.dropTable(connectionSource, CloudDiskUploadFile.class, true);
            TableUtils.dropTable(connectionSource, Contact.class, true);
            TableUtils.dropTable(connectionSource, Department.class, true);
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            onCreate(sqLiteDatabase, connectionSource);
        }
    }

    public Dao<User,Integer> getUserDao() throws  SQLException{
        if(userDao ==null){
            userDao = getDao(User.class);
        }
        return userDao;
    }

    public Dao<CloudDisk,Integer> getCloudDiskDao() throws  SQLException{
        if(cloudDiskDao ==null){
            cloudDiskDao = getDao(CloudDisk.class);
        }
        return cloudDiskDao;
    }

    public Dao<CloudDiskUploadFile,Integer> getCloudDiskUploadFileDao() throws  SQLException{
        if(cloudDiskUploadFileDao ==null){
            cloudDiskUploadFileDao = getDao(CloudDiskUploadFile.class);
        }
        return cloudDiskUploadFileDao;
    }

    public Dao<Contact, Integer> getContactDao() throws  SQLException{
        if(contactDao == null){
            contactDao = getDao(Contact.class);
        }
        return contactDao;
    }

    public Dao<Department, Integer> getDepartmentDao() throws  SQLException{
        if(departmentDao == null){
            departmentDao = getDao(Department.class);
        }
        return departmentDao;
    }
}
